package getOffer.offer62;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wjy
 * @Date: 2021/11/18 14:31
 */
public class Solution {

    public static void main(String[] args) {

        Solution solution = new Solution();

/*        List<Integer> list = new ArrayList<>();
        list.add(10);
        list.add(9);
        list.add(8);
        System.out.println(list.remove(0));
        System.out.println(list.get(0));*/

        System.out.println(solution.lastRemaining(5, 3));

    }

    public int lastRemaining(int n, int m) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(i);
        }

        int index = 0;
        while (n > 1) {
            index = (index + m - 1) % n;
            list.remove(index);
            n--;
        }

        return list.get(0);
    }

}
